/*
 * menu.c
 *
 * Created: 04.10.2012 10:27:13
 *  Author: vegardvo
 */ 

#include "menu.h"
#include "oledDriver.h"
#include "inputDriver.h"
#include "definitions.h"
#include <string.h>
#include <util/delay.h>

int menu_show_menu(char items[16][16], int size, char title[], int highlightedIndex)
{
	int startIndex = 0;
	//int highlightedIndex = 0;
	struct joystickDirection joystickDir;
	struct joystickDirection tempDir;
	
	if(highlightedIndex > 6) 
	{
		startIndex = highlightedIndex - 6;
	}
	
	menu_print_menu(items, size, title, startIndex, highlightedIndex);
	
	while(TRUE)
	{
		joystickDir = inputDriver_joystick_getDir();
		
		if(joystickDir.y_dir == UP)
		{
			// highlightedIndex:
			if(highlightedIndex > 0)
			{
				highlightedIndex--;
			}
			
			// startIndex
			if(highlightedIndex < startIndex)
			{
				startIndex--;
			}
			menu_print_menu(items, size, title, startIndex, highlightedIndex);
			_delay_ms(70);
		}
		else if(joystickDir.y_dir == DOWN)
		{
			// highlightedIndex:
			if(highlightedIndex < size - 1)
			{
				highlightedIndex++;
			}
		
			// startIndex
			if(highlightedIndex > startIndex + 6)
			{
				startIndex++;
			}
			menu_print_menu(items, size, title, startIndex, highlightedIndex);
			_delay_ms(70);
		}
		else if(joystickDir.x_dir == RIGHT)
		{
			return highlightedIndex;
		}
		else if(joystickDir.x_dir == LEFT)
		{
			return -1;
		}
	}
		
}

void menu_print_menu(char items[16][16], int size, char title[], int startIndex, int highlitedIndex)
{
	oledDriver_clear_screen();
	oledDriver_select_page(0);
	oledDriver_write_string(title, FALSE, TRUE);
	
	int endIndex = startIndex + 7;
	if(size < endIndex)
	{
		endIndex = size;
	}
	int page = 1;
	for(int i = startIndex; i < endIndex; i++)
	{
		oledDriver_select_page(page);
		oledDriver_write_string(items[i], i == highlitedIndex, FALSE);
		page++;
	}
	
}

void menu_main_menu()
{
	int highlightedIndex = 0;
	char menuItems[9][16] = {"Start game", "Options", "High score", "Ho ho ho", "Buy boots", "Buy drugs", "Shoot cops", "Kick ass!", "Take names"};
	while(TRUE)
	{
		
		int temp = menu_show_menu(menuItems, 9, "Main menu", highlightedIndex);
		_delay_ms(100);
		if(temp != -1)
		{
			highlightedIndex = temp;
		}
		switch(temp)
		{
			case 0: 
				//pingPong_startGame();
				break;
			case 1: 
				menu_options_menu();
				break;
			case 2:
				menu_highScore();
				break;
			default:
				break;
		}
	}	
	
}

void menu_options_menu()
{
	int highlightedIndex = 0;
	int size = 3;
	char menuItems[3][16] = {"No options here", "fooled you!", "haha"};
	while(TRUE)
	{
		
		highlightedIndex = menu_show_menu(menuItems, size, "Options", highlightedIndex);
		_delay_ms(70);
		switch(highlightedIndex)
		{
			case 0:
				
				break;
			case 1:
				
				break;
			case 2:
				
				break;
			default:
				return;
				break;
		}
	}
}

void menu_highScore()
{
	int highlightedIndex = 0;
	int size = 3;
	char menuItems[3][16] = {"tr: 999999", "vv: 72", "th: 72"};
	while(TRUE)
	{
		
		highlightedIndex = menu_show_menu(menuItems, size, "High score", highlightedIndex);
		_delay_ms(70);
		switch(highlightedIndex)
		{
			case 0:
					
				break;
			case 1:
			
				break;
			case 2:
				
				break;
			default:
				return;
				break;
		}
	}
	
}